package com.cl.it.spider;

import com.cl.it.mbg.entity.Company;
import com.cl.it.mbg.entity.News;
import com.cl.it.utils.DateUtils;
import com.cl.it.utils.StringUtils;
import com.cl.it.utils.UrlUtils;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component
@Slf4j
public class ToutiaoSpider extends AbstractSpider {


    @Override
    protected void executeSingleCompany(WebDriver driver, WebDriverWait wait, Company company) {
        String url = "https://so.toutiao.com/search?keyword=###&pd=information&source=search_subtab_switch&dvpf=pc&aid=4916&page_num=0";
        url = url.replace("###", company.getShortName());
        driver.get(url);
        sleep(1000L);
        String listCssSelector = "div.s-result-list>div.result-content";
        if (!has(driver, listCssSelector)) {
            return;
        }
        List<WebElement> articles = selectors(driver, listCssSelector);
        if (CollectionUtils.isEmpty(articles)) {
            return;
        }
        SeleniumSpider spider = new SeleniumSpider(chomeDriver);
        for (int i = 0; i < articles.size() - 1; i++) {
            WebElement element = articles.get(i);
            News news = new News();
            news.setName(company.getName());
            news.setTitle(selectorText(element, "div.cs-card-content>:nth-child(1)"));
            news.setContent(selectorText(element, "div.cs-card-content>:nth-child(2)>:nth-child(2)>:nth-child(1)"));
            news.setSource(selectorText(element, "div.cs-card-content>:nth-child(2)>:nth-child(2)>:nth-child(2)>:nth-child(1)>:nth-child(1)>:nth-child(1)"));
            news.setSourceUrl(UrlUtils.nextUrl(selector(element, "div.cs-card-content>:nth-child(1) a").getAttribute("href"), "url"));
            news.setNewsDate(DateUtils.dateConvert(selectorText(element, "div.cs-card-content>:nth-child(2)>:nth-child(2)>:nth-child(2)>:nth-child(1)>:nth-child(1)>:nth-child(3)")));


            spider.getDriver().get(news.getSourceUrl());
            sleep(1000L);

            String digText = selectorText(spider.getDriver(), "div.left-sidebar div[role=complementary][aria-label='工具栏'] div[role=button][aria-label*='点赞']");
            news.setDigCount(StringUtils.toLong(StringUtils.regx(digText, "(\\d+)")));
            String commentText = selectorText(spider.getDriver(), "div.left-sidebar div[role=complementary][aria-label='工具栏'] div[role=button][aria-label*='评论']");
            news.setCommentCount(StringUtils.toLong(StringUtils.regx(commentText, "(\\d+)")));
            String favoriteText = selectorText(spider.getDriver(), "div.left-sidebar div[role=complementary][aria-label='工具栏'] div[role=button][aria-label*='收藏']");
            news.setFavoriteCount(StringUtils.toLong(StringUtils.regx(favoriteText, "(\\d+)")));
            String newsDateText = selectorHtml(spider.getDriver(), "div.article-meta");
            news.setNewsDate(StringUtils.regx(newsDateText, "<span>(.*?)</span>"));
            news.setDataSource("今日头条");

            UpdateWrapper<News> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("title", news.getTitle());
            updateWrapper.eq("name", news.getName());
            newsService.saveOrUpdate(news, updateWrapper);
        }
        spider.getDriver().quit();
    }

    @Override
    protected void login(WebDriver driver, WebDriverWait wait) {

    }
}
